import { Component, OnInit } from '@angular/core';
import { CveService } from 'src/app/core/service/cve.service';

@Component({
  selector: 'app-threat-list',
  templateUrl: './threat-list.component.html',
  styleUrls: ['./threat-list.component.scss']
})
export class ThreadListComponent implements OnInit {

  allChecked = false;
  indeterminate = false;
  displayData = [];

  filter = '';

  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页

  dataSet = [];       // 数据集合

  loading = true;     // 加载

  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  // 按性别查询
  filterGender = [
    { text: 'male', value: 'male' },
    { text: 'female', value: 'female' }
  ];
  searchGenderList: string[] = [];

  sort(sort: { key: string, value: string }): void {
    this.sortKey = sort.key;
    this.sortValue = sort.value;
    // this.searchData();
  }

  constructor(private cveservice: CveService
  ) { }

  /**
   * 搜索数据
   * @param reset 重置
   */
  /* searchData(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;
    this.threelibsservice.getUsers(this.pageIndex, this.pageSize, this.sortKey, this.sortValue,
      this.searchGenderList).subscribe((data: any) => {
        this.loading = false;
        this.total = 200;
        this.dataSet = data.results;
      });
  } */

  /**
     *
     * @param reset 获取数据
     */
  getCVEPage(reset: boolean = false): void {
    console.log('==Janine.Project.list==>进入');
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;
    this.cveservice.getCVEPage(this.pageIndex, this.pageSize, this.filter)
      .subscribe((cvePage) => {
        this.loading = false;
        console.log('===收到cvePage数据===>', cvePage);
        if (cvePage !== null && cvePage !== undefined) {
          this.dataSet = cvePage.data;
          this.total = cvePage.total;
          console.log('===cvePage现在的数据===', cvePage.data);
        } else {
          console.log('暂无数据');
        }

      });
  }

  /* getCVEFilterPage(reset: boolean = false): void {
    console.log('==Janine.Project.list==>进入');
    if (this.filter.length > 1 && this.filter.length < 50) {
      if (reset) {
        this.pageIndex = 1;
      }
      this.cveservice.getCVEFilterPage(this.filter)
        .subscribe((cvePage) => {
          console.log(`===收到cvePage数据===> ${cvePage}`);
          this.dataSet = cvePage.data;
          this.total = cvePage.total;
          console.log('===cvePage现在的数据===', cvePage.data);
        });
    }
  } */

  updateFilter(value: string[]): void {
    this.searchGenderList = value;
    // this.searchData(true);
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }


  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  /**
   * 全选方法
   */
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    // 刷新状态
    this.refreshStatus();
  }

  ngOnInit(): void {
    // this.searchData();
    this.getCVEPage();
  }

}
